package com.server.llh.llhserverwork.activity.server;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.family.llh.lelaohuitoastview.dialog.toast.T;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lelaohui.net.BaseLeLaohuiAcitivity;
import com.lelaohui.net.listener.NetDataListener;
import com.lelaohui.net.netmanager.INetRequestManager;
import com.lelaohui.net.netmanager.ServerNetRequestManager;
import com.lelaohui.net.request.LRequest;
import com.lelaohui.net.utils.UrlsTools;
import com.llh.llhsuppliers.R;
import com.server.llh.llhserverwork.activity.query.QueryUserCardInfoActivity;
import com.server.llh.llhserverwork.adapter.LLHServerAdapter;
import com.server.llh.llhserverwork.bean.Constants;
import com.server.llh.llhserverwork.bean.ProCateService;
import com.server.llh.llhserverwork.utils.ServerPort;
import com.server.llh.llhserverwork.utils.SharedPreferenceUtil;
import com.server.llh.llhserverwork.view.MyEpListPopuView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LlhServerActivityActivity extends BaseLeLaohuiAcitivity implements MyEpListPopuView.GvItemClickCallBack, BottomNavigationBar.OnTabSelectedListener {
    private static final String TAG = "LlhServer";
    @BindView(R.id.main_back)
    ImageButton leftBtn;
    @BindView(R.id.titleName)
    TextView titlebarText;

    @BindView(R.id.server_menu)
    GridView serverMenu;
    @BindView(R.id.reloadbt)
    ImageView reloadbt;
    @BindView(R.id.btn)
    Button btn;
    @BindView(R.id.bottom_navigation_bar)
    BottomNavigationBar bottomNavigationBar;
    private String packorgTypeId, packorgId,chainId, mtoken;
    private MyEpListPopuView myEpListPopuView = null;
    private MyEpListPopuView.GvItemClickCallBack gvItemClickCallBack = null;
    MyBroadFinishActivity myBroadFinishActivity;
    private String  intentTag="0";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

        chainId=SharedPreferenceUtil.getUserInfo(Constants.UserInfo.CHAIN_ID,getApplicationContext());
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Constants.BroadCastAction.FINISH_ALL_ACTIVITY);
        myBroadFinishActivity = new MyBroadFinishActivity();
        this.registerReceiver(myBroadFinishActivity, intentFilter);
        gvItemClickCallBack = this;
        titlebarText.setText("购买服务");
        mtoken = SharedPreferenceUtil.getUserInfo(Constants.UserInfo.TOKEN_STR, getApplicationContext());
        packorgId = SharedPreferenceUtil.getUserInfo(Constants.UserInfo.ORG_ID, getApplicationContext());
        packorgTypeId = SharedPreferenceUtil.getUserInfo(Constants.UserInfo.ORG_TYPE, getApplicationContext());
        reqServerMenu(packorgId, packorgTypeId);
        if (getIntent()!=null){
            intentTag=getIntent().getStringExtra("intentTag");
            if (!"0".equals(intentTag)){
                bottomNavigationBar.setVisibility(View.GONE);
                titlebarText.setText("选择服务");
            }
        }
        bottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);
        bottomNavigationBar
                .setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC
                );
        bottomNavigationBar.addItem(new BottomNavigationItem(R.mipmap.llh_nav_1, "首页").setActiveColorResource(R.color.orange1))
                .addItem(new BottomNavigationItem(R.mipmap.llh_nav_2, "服务预约").setActiveColorResource(R.color.teal))
                .addItem(new BottomNavigationItem(R.mipmap.llh_nav_3, "服务订单").setActiveColorResource(R.color.blue1))
                .setFirstSelectedPosition(0)
                .initialise();
        bottomNavigationBar.setTabSelectedListener(this);

        serverMenu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
             Intent intent;
                if ("0".equals(intentTag)){
                    intent = new Intent(getApplicationContext(), LlhServerMenuTwoActivity.class);
                }else{
                    intent = new Intent(getApplicationContext(), LlhServerOptionActivity.class);
                }                String mCateName = service.get(position).getCateName();
                Long mCateId = service.get(position).getCateId();
                int mCateLevel = service.get(position).getCateLevel();
                int mIsPack = service.get(position).getIsPack();
                intent.putExtra("cateName", mCateName);
                intent.putExtra("cateId", mCateId + "");
                intent.putExtra("cateLevel", mCateLevel + "");
                intent.putExtra("mIsPack", mIsPack + "");
                startActivity(intent);
            }
        });
        reloadbt.setVisibility(View.VISIBLE);
    }

    private class MyBroadFinishActivity extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (Constants.BroadCastAction.FINISH_ALL_ACTIVITY.equals(action)) {
                finish();
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        reloadbt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                reqServerMenu(packorgId, packorgTypeId);
            }
        });
    }

    @Override
    protected void doBasess() {

    }

    LLHServerAdapter llhServerAdapter = null;

    private List<ProCateService> service = new ArrayList<>();

    @Override
    public void callBack(String string) {
//        ToastTool.showText(getApplicationContext(), "啦啦啦啦===" + string);
    }

    @OnClick(R.id.btn)
    public void onViewClicked() {
        setData();
        myEpListPopuView = new MyEpListPopuView(LlhServerActivityActivity.this, itemList, groupList, gvItemClickCallBack);
        myEpListPopuView.showAsDropDown(btn, btn.getLayoutParams().width / 2, 18);
    }

    /**
     * 每个分组的名字的集合
     */
    private List<String> groupList;

    /**
     * 每个分组下的每个子项的 GridView 数据集合
     */
    private List<String> itemGridList;

    /**
     * 所有分组的所有子项的 GridView 数据集合
     */
    private List<List<String>> itemList;

    public void setData() {
        // 分组
        groupList = new ArrayList<>();
        groupList.add("服务---");
        groupList.add("服务===");
        // 每个分组下的每个子项的 GridView 数据集合
        itemGridList = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            itemGridList.add("乐老汇" + (i + 1));
        }
        // 所有分组的所有子项的 GridView 数据集合
        itemList = new ArrayList<>();
        itemList.add(itemGridList);
        itemList.add(itemGridList);
    }


    @Override
    public void onTabSelected(int position) {
        Intent intent = null;
        if (position == 0) {
            onBackPressed();
        } else if (position == 1) {

            intent = new Intent(getApplicationContext(), QueryUserCardInfoActivity.class);
            intent.putExtra("intentTag","0");
        } else {
            intent = new Intent(getApplicationContext(), QueryUserCardInfoActivity.class);
            intent.putExtra("intentTag","1");

        }
        if (intent != null) {
            startActivity(intent);
        }
    }

    @Override
    public void onTabUnselected(int position) {
    }

    @Override
    public void onTabReselected(int position) {
    }

    public void showProgress() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                try {
                    showProgress(LlhServerActivityActivity.this);
                } catch (Exception e) {
                }
            }
        });
    }

    public void hideProgress() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                try {
                    closeProgress();
                } catch (Exception e) {
                }
            }
        });
    }

    protected int getlayout() {
        return R.layout.activity_server_menu;
    }

    @Override
    protected void initView() {
    }

    protected INetRequestManager getRequestManager() {
        return ServerNetRequestManager.getInstance();
    }


    @OnClick(R.id.main_back)
    public void onClick() {
        onBackPressed();
    }

    private void reqServerMenu(String packorgId, String packorgTypeId) {
        LRequest lRequest = new LRequest();
        lRequest.addParam("cateLevel", 0);
        lRequest.addParam("isPack", 0);
        lRequest.addParam("packorgId", chainId);
        lRequest.addParam("token", mtoken);
        lRequest.addParam("packorgTypeId", "3");
        if (!"0".equals(intentTag)){
            lRequest.addParam("isPro", "2");
        }
        lRequest.addParam("isEmptyShow", Constants.IS_EMPTY_SHOW_INT);
        lRequest.addParam("packStatus", Constants.PACK_STATUS_INT);
        lRequest.setUri(ServerPort.GET_SERVER_ON_TYPE);
        lRequest.setIp(UrlsTools.SERVER_REQ_URL);
        lRequest.setType(String.class);
        requestData(lRequest, new NetDataListener<String>() {
            @Override
            public void onError(Throwable t) {
                reloadbt.setVisibility(View.VISIBLE);
                T.showAnimErrorToast(getApplicationContext(), "请求超时！");
            }

            @Override
            public void onNext(String responseBean, String code) {
                Log.d(TAG, "responseBean==" + responseBean.toString());
                if (responseBean != null && Constants.SER_RESPON_SUCC_CODE.equals(code)) {
                    service = new Gson().fromJson(responseBean.toString(), new TypeToken<List<ProCateService>>() {
                    }.getType());
                    if (service != null && service.size() > 0) {
                        reloadbt.setVisibility(View.GONE);
                        llhServerAdapter = new LLHServerAdapter(getApplicationContext(), service);
                        serverMenu.setAdapter(llhServerAdapter);
                    } else {
                        reloadbt.setVisibility(View.VISIBLE);
                    }
                } else {
                    reloadbt.setVisibility(View.INVISIBLE);
                    T.showAnimToast(getApplicationContext(), "暂时没有数据！");
                }
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (myBroadFinishActivity != null) {
            unregisterReceiver(myBroadFinishActivity);
        }
    }
}